﻿//using System;
//using System.Collections.Generic;
//using System.Diagnostics;
//using System.IO;
//using System.Linq;
//using System.Text;
//using SysProcess = System.Diagnostics.Process;

//namespace System.Diagnostics
//{
//	/// <summary>
//	/// Helper class for running external processes.
//	/// </summary>
//	public static class _Process
//	{
//		#region Constants
//		private const bool Default_WaitForExit = true;
//		private const bool Default_RedirectOutput = false;
//		private const string Default_WorkingDirectory = null;
//		#endregion

//		#region Methods - Run
//		public static SysProcess Run(string exe, string args)
//		{
//			return DoRun(exe, args, GetWorkingDirectory(Default_WorkingDirectory), Default_WaitForExit, Default_RedirectOutput);
//		}

//		public static SysProcess Run(string exe, string args, bool waitForExit)
//		{
//			return DoRun(exe, args, GetWorkingDirectory(Default_WorkingDirectory), waitForExit, Default_RedirectOutput);
//		}

//		public static SysProcess Run(string exe, string args, bool waitForExit, bool redirectOutput)
//		{
//			return DoRun(exe, args, GetWorkingDirectory(Default_WorkingDirectory), waitForExit, redirectOutput);
//		}

//		public static SysProcess Run(string exe, string args, string workingDirectory)
//		{
//			return DoRun(exe, args, GetWorkingDirectory(workingDirectory), Default_WaitForExit, Default_RedirectOutput);
//		}

//		public static SysProcess Run(string exe, string args, string workingDirectory, bool waitForExit)
//		{
//			return DoRun(exe, args, GetWorkingDirectory(workingDirectory), waitForExit, Default_RedirectOutput);
//		}

//		public static SysProcess Run(string exe, string args, string workingDirectory, bool waitForExit, bool redirectOutput)
//		{
//			return DoRun(exe, args, GetWorkingDirectory(workingDirectory), waitForExit, redirectOutput);
//		}

//		/// <summary>
//		/// Gets a valid value for a working directory.
//		/// </summary>
//		/// <param name="workingDirectory">The working directory to initially attempt to use.</param>
//		/// <returns>A valid working directory, or the current directory is the specified one is not a valid path.</returns>
//		private static string GetWorkingDirectory(string workingDirectory)
//		{
//			if (_String.IsNullOrWhiteSpace(workingDirectory))
//			{
//				return Directory.GetCurrentDirectory();
//			}
//			else
//			{
//				try
//				{
//					return new Folder(workingDirectory).FullName;
//				}
//				catch
//				{
//					return Directory.GetCurrentDirectory();
//				}
//			}
//		}

//		private static SysProcess DoRun(string exe, string args, string workingDirectory, bool waitForExit, bool redirectOutput)
//		{
//			try
//			{
//				var info = new ProcessStartInfo(exe, args);
//				info.RedirectStandardOutput = redirectOutput;
//				info.RedirectStandardError = redirectOutput;
//				info.UseShellExecute = false;
//				info.WorkingDirectory = workingDirectory;

//				var process = new SysProcess();
//				process.StartInfo = info;

//				if (redirectOutput)
//				{
//					process.OutputDataReceived += process_OutputDataReceived;
//					process.ErrorDataReceived += process_ErrorDataReceived;
//				}

//				process.Start();

//				if (redirectOutput)
//				{
//					process.BeginOutputReadLine();
//					process.BeginErrorReadLine();
//				}

//				if (waitForExit) process.WaitForExit();

//				return process;
//			}
//			catch (Exception exc)
//			{
//				throw new Exception(string.Format("Process failed: '{0}' '{1}'", exe, args), exc);
//			}
//		}
//		#endregion

//		#region Methods - Process Output Redirection
//		private static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
//		{
//			if (!_String.IsNullOrWhiteSpace(e.Data))
//			{
//				string filename = Path.GetFileName((sender as SysProcess).StartInfo.FileName);
//				Log.WriteLine("Process Output Redirect - {0}: {1}", filename, e.Data);
//			}
//		}

//		private static void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
//		{
//			if (!_String.IsNullOrWhiteSpace(e.Data))
//			{
//				string filename = Path.GetFileName((sender as SysProcess).StartInfo.FileName);
//				Log.WriteLine("Process Error Redirect  - {0}: {1}", filename, e.Data);
//			}
//		}
//		#endregion
//	}
//}
